package com.smart.gateway.handler;

import com.rabbitmq.client.Channel;
import com.smart.core.starter.constant.MqQueueConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 路由刷新配置
 *
 * @author guxiaobai
 * @date 2018/12/11
 */
@Component
@Slf4j
public class RouterRefreshHandler {


    @Autowired
    private RefreshRouteService refreshRouteService;

    @RabbitListener(queues = {MqQueueConstant.ROUTE_CONFIG_CHANGE})
    public void listenerRouterRefreshAck(Boolean refresh, Message message, Channel channel) {
        try {
            log.info("[listenerRouterRefreshAck 监听的消息] - [{}]", refresh);
            if (refresh) {
                refreshRouteService.refreshRoute();
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.error("路由刷新失败");
        }
    }

}
